package com.hzh.cusviewdemo.Music;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.blankj.utilcode.util.TimeUtils;
import com.hzh.cusviewdemo.Activity.MusicActivity;
import com.hzh.cusviewdemo.Presenter.MusicSongData;
import com.hzh.cusviewdemo.Util.Config;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

//此线程只是用于循环发送广播，通知更改歌曲播放进度。
public class UpdateUIThread extends Thread {

    private int threadNumber;
    private Context context;
    private PlayerManagerReceiver playerManagerReceiver;
    private int duration;
    private int curPosition;
    private String musicTitle;
    private List<MusicSongData> songDataList;

    public UpdateUIThread(PlayerManagerReceiver playerManagerReceiver, String musicTitle, Context context, int threadNumber) {
        this(playerManagerReceiver, musicTitle, null, context, threadNumber);
        Log.i(Config.TAG, "UpdateUIThread: ");
    }

    public UpdateUIThread(PlayerManagerReceiver playerManagerReceiver, String musicTitle, List<MusicSongData> songData, Context context, int threadNumber) {
        Log.i(Config.TAG, "UpdateUIThread: ");
        this.playerManagerReceiver = playerManagerReceiver;
        this.context = context;
        this.threadNumber = threadNumber;
        this.songDataList = songData;
        this.musicTitle = musicTitle;
    }


    @Override
    public void run() {
        try {
            while (playerManagerReceiver.getThreadNumber() == this.threadNumber) {
                if (playerManagerReceiver.status == Constant.STATUS_STOP) {
                    Log.e(Config.TAG, "run: Constant.STATUS_STOP");
                    break;
                }
                if (playerManagerReceiver.status == Constant.STATUS_PLAY || playerManagerReceiver.status == Constant.STATUS_PAUSE) {
                    if (!playerManagerReceiver.getMediaPlayer().isPlaying()) {
                        Log.i(Config.TAG, "run: getMediaPlayer().isPlaying() = " + playerManagerReceiver.getMediaPlayer().isPlaying() + threadNumber + "---" + playerManagerReceiver.getThreadNumber());
                        break;
                    }

                    // 播放中
                    duration = playerManagerReceiver.getMediaPlayer().getDuration();
                    curPosition = playerManagerReceiver.getMediaPlayer().getCurrentPosition();
                    Intent intent = new Intent(MusicActivity.ACTION_UPDATE_UI_MUSIC);
                    intent.putExtra(Constant.STATUS, Constant.STATUS_RUN);
                    intent.putExtra(Constant.KEY_DURATION, duration);
                    intent.putExtra(Constant.KEY_CURRENT, curPosition);
//                    if (songDataList != null) {
                         intent.putExtra(Constant.KEY_CURRENT_SENTENCE, getNowMusicSentence(curPosition));
//                    }
                    context.sendBroadcast(intent);
                }

//                if (playerManagerReceiver.status == Constant.STATUS_PAUSE) {
//                    duration = playerManagerReceiver.getMediaPlayer().getDuration();
//                    curPosition = playerManagerReceiver.getMediaPlayer().getCurrentPosition();
//                    Intent intent = new Intent(MusicActivity.ACTION_UPDATE_UI_MUSIC);
//                    intent.putExtra(Constant.STATUS, Constant.STATUS_PAUSE);
//                    intent.putExtra(Constant.KEY_DURATION, duration);
//                    intent.putExtra(Constant.KEY_CURRENT, curPosition);
//                    context.sendBroadcast(intent);
//                }
                // 延迟一秒后再循环
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String getNowMusicSentence(int curPosition) {
        String result = musicTitle;
        for (MusicSongData musicSongData : songDataList) {
            double songTime = Double.parseDouble(musicSongData.getTime());
            double nowTime = int2double(curPosition);  //  + 0.2
//            Log.e(Config.TAG, "UUT==:" + songTime + "----" + int2double(curPosition));
            if (nowTime >= songTime) {
                result = musicSongData.getLineLyric();
            }
        }
//        Log.e(Config.TAG, "UUT==:" + result);
        return result;
    }

    /**
     * 分转元，转换为bigDecimal在toString
     *
     * @return
     */
    public static double int2double(int num) {
        return BigDecimal.valueOf(num * 1.0).divide(new BigDecimal(1000)).doubleValue();
    }

}
